package com.pz.art.service.impl;

import com.pz.art.mapper.AssoMemberApplyEventsMapper;
import com.pz.art.mapper.EventsMapper;
import com.pz.art.domain.AssoEvents;
import com.pz.art.service.EventsService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Primary
public class EventsServiceImpl implements EventsService {

    @Autowired
    private EventsMapper eventsMapper;

    @Autowired
    private AssoMemberApplyEventsMapper assoMemberApplyEventsMapper;

    /**
     * 查询活动
     *
     * @param id 活动主键
     * @return 活动
     */
    @Override
    public AssoEvents selectAssoEventsById(Long id) {
        return eventsMapper.selectAssoEventsById(id);
    }


    /**
     * 查询活动列表
     *
     * @param assoEvents 活动
     * @return 活动
     */
    @Override
    public List<AssoEvents> selectAssoEventsList(AssoEvents assoEvents) {
        return eventsMapper.selectAssoEventsList(assoEvents);
    }

    @Override
    public List<AssoEvents> selectApplyAssoEventsList(AssoEvents assoEvents, Long unit_id) {
        return eventsMapper.selectApplyAssoEventsList(assoEvents, unit_id);
    }

    /**
     * 新增活动
     *
     * @param assoEvents 活动
     * @return 结果
     */
    @Override
    public int insertAssoEvents(AssoEvents assoEvents) {
        assoEvents.setCreateTime(DateUtils.getNowDate());
        assoEvents.setType(2);
        if (assoEvents.getUnit_id() == null) {
            assoEvents.setUnit_id(0L);
        }
        assoEvents.setSign_unit(0L);
        //活动状态设置为
        assoEvents.setActivity_status(5);
        assoEvents.setJoin_num(0L);
        assoEvents.setCreate_time(DateUtils.getNowDate());
        //设置为待审核
        assoEvents.setExamine_status(1);
        System.out.println(assoEvents);
        return eventsMapper.insertAssoEvents(assoEvents);
    }

    /**
     * 修改活动
     *
     * @return 结果
     */
    @Override
    public AjaxResult updateAssoEvents(Long events_id) {
        AjaxResult ajaxResult = new AjaxResult();
        System.out.println(1111);
        AssoEvents events = eventsMapper.selectAssoEventsById(events_id);
        if (events.getActivity_status() == 5){
            ajaxResult.put("msg", "当前活动正在审核中，不能结束");
            ajaxResult.put("code", "520");
        } else if (events.getActivity_status() == 4) {
            ajaxResult.put("code", "520");
            ajaxResult.put("msg", "当前活动已结束");
        } else {
            eventsMapper.updateAssoEventsStatus(events_id);
            ajaxResult.put("msg", "活动状态修改成功,成功结束");
        }
        System.out.println(ajaxResult);
        return ajaxResult;
    }

    /**
     * 批量删除活动
     *
     * @param ids 需要删除的活动主键
     * @return 结果
     */
    @Override
    public int deleteAssoEventsByIds(Long[] ids) {
        return eventsMapper.deleteAssoEventsByIds(ids);
    }

    /**
     * 删除活动信息
     *
     * @param id 活动主键
     * @return 结果
     */
    @Override
    public int deleteAssoEventsById(Long id) {
        return eventsMapper.deleteAssoEventsById(id);
    }

    @Override
    public List<AssoEvents> getEventsByUnitId(Long unit_id) {
        return eventsMapper.getEventsByUnitId(unit_id);
    }

    @Override
    public int addunitevents(AssoEvents assoEvents, Long unit_id) {
        assoEvents.setCreateTime(DateUtils.getNowDate());
        assoEvents.setUnit_id(unit_id);
        assoEvents.setType(2);
        assoEvents.setSign_unit(0L);
        //活动状态设置为
        assoEvents.setActivity_status(5);
        assoEvents.setJoin_num(0L);
        //设置为待审核
        assoEvents.setExamine_status(1);
        eventsMapper.insertAssoEvents(assoEvents);
        Long events_id = assoEvents.getId();
        return eventsMapper.inAssoApplyInfo(events_id, unit_id, DateUtils.getNowDate());
    }

    @Override
    public List<AssoEvents> selectApplyStatusAssoEventsList(AssoEvents assoEvents) {
        return eventsMapper.selectApplyStatusAssoEventsList(assoEvents);
    }

    @Override
    public int examineevents(AssoEvents assoEvents) {
        assoEvents.setUpdate_time(DateUtils.getNowDate());
        return eventsMapper.examineevents(assoEvents);
    }

    @Override
    public int addonAssortments(Map<String,String>  eventsIds) {
        Integer events_ids = 0;
        List<String> unit_member_id = new ArrayList<>();
        for (Map.Entry<String, String> entry : eventsIds.entrySet()) {
            if (entry.getKey().equals("events_ids")){
                events_ids = Integer.parseInt(entry.getValue());
            } else {
                unit_member_id.add(entry.getValue());
            }
        }

        assoMemberApplyEventsMapper.toaddmember(events_ids,unit_member_id);
        return eventsMapper.toaddeventsmember(events_ids,unit_member_id.size());
    }
}
